<?php
/*
 * This class is used for as Model for Story table 
 */
class Model_Story extends Model_Base {
    protected $_name = "tbl_stories";
    protected $_primaryKey = "st_id";
     
    public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array("st" => "tbl_stories"))
                ->setIntegrityCheck(false)
                ->join(array("proj" => "tbl_project"), "proj.proj_id = st.st_proj_id",array("proj_title"))
                ->where("st_id = ?", $id)
                ->where("st_status!= 2");
        $data = $this->fetchRow($select);
       # echo $select;exit;
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get story
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchStory($where = "", $limit = "", $offset = "0", $order = "st_id", $sort = "ASC") {
        $select = $this->select()
                ->from(array("st" => "tbl_stories"))
                ->setIntegrityCheck(false)
                ->join(array("proj" => "tbl_project"), "proj.proj_id = st.st_proj_id",array("proj_title"))
                ->join(array("user" => "tbl_user"), "user.u_lgn_id = st.st_lgn_id",array("u_name","u_username"))
                
                ->where("st_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getStoryCount($where) {
        $select = $this->select()
                ->from(array("st" => "tbl_stories"), Array("cnt" => "count(*)"))
                ->setIntegrityCheck(false)
                ->join(array("proj" => "tbl_project"), "proj.proj_id = st.st_proj_id",array("proj_title"))
                ->where("st_status != 2");
        if ($where != "") {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    /**
     *  This function is used to delete record
     * 
     */
    public function deleteStory($stArray) {
        if (is_array($stArray)) {
            foreach ($stArray as $_story) {
                $storyData = Array();
                $storyData = $this->fetchEntryById($_story);
                if (count($storyData) > 0) {
                    $data = array("st_status" => "2");
                    $this->update($data, "st_id =" . (int) $_story);
                }
            }
        } else {
            $st_id = $stArray;
            $storyData = $this->fetchEntryById($st_id);
            if (count($storyData) > 0) {
                $data = array("st_status" => "2");
                $this->update($data, "st_id =" . $st_id);
            }
        }
    }


}?>